<?php
/*
  	CakePHP Facebook - Helper to CakePHP framework that integrates a Facebook in your view
  	using Facebook Api, Facebook Graph Api and Facebook JavaScript SDX.
  
	Copyright (c) 
	
	MIT LICENSE:
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
  

	@author     
	@version     1.0
	@license     OPPL
	 
	Date	    
    USAGE:
    
 
*/

	
class FacebookHelper extends AppHelper {
	

	function get_facebook_cookie($app_id, $application_secret) {
	if($this->isLoggedIn()) {
	  $args = array();
	  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
	  ksort($args);
	  $payload = '';
	  foreach ($args as $key => $value) {
	    if ($key != 'sig') {
	      $payload .= $key . '=' . $value;
	    }
	  }
	  if (md5($payload . $application_secret) != $args['sig']) {
	    return null;
	  }
	  return $args;}
	  else {}
	}
	
	function isLoggedIn() {
		$app_id = '117520721607819';
		if(isset($_COOKIE['fbs_' . $app_id])) { return true; }
		else { return false; }
	}
	
	function get_cookie() {
		$FACEBOOK_APP_ID = '117520721607819';
		$FACEBOOK_SECRET = '2b774e4b893c6686fff65002597be42e';
		$cookie = $this->get_facebook_cookie($FACEBOOK_APP_ID, $FACEBOOK_SECRET);
		return $cookie;
	}
	
	function get_id() {
		$cookie = $this->get_cookie();
		$id = $cookie['uid'];
		return $id;
	}
	
	function get_access_token() {
		$cookie = $this->get_cookie();
		$access_token = $cookie['access_token'];
		return $access_token;
	}
	
	function get_jelly_friends() {
		$access = $this->get_access_token();
		$appfriends = json_decode(file_get_contents('https://api.facebook.com/method/friends.getAppUsers?access_token='. $access . '&format=json'));
		return $appfriends;
	}
	
	function get_profile() {
		$access = $this->get_access_token();
		$profile = json_decode(file_get_contents('https://graph.facebook.com/me?access_token=' . $access));
		return $profile;
	}
	
	function get_photo_from_id($pid) {
		$access = $this->get_access_token();
		$photo = json_decode(file_get_contents('https://graph.facebook.com/me/photos?access_token=' . $access . ''));
	
		$data = $photo->data;
		for($i = 0; $i < 5000; $i++){
			if($data[$i]->id == $pid) {
				$source = $data[$i]->source;
				break;
				}
			}
		return $source;
	
	}
	
	function get_thumb_from_id($pid) {
		$access = $this->get_access_token();
		$photo = json_decode(file_get_contents('https://graph.facebook.com/me/photos?access_token=' . $access . ''));
	
		$data = $photo->data;
		for($i = 0; $i < 5000; $i++){
			if($data[$i]->id == $pid) {
				$source = $data[$i]->picture;
				break;
				}
			}
		return $source;
	
	}
	
	function get_profile_from_id($fid) {
		$profile = json_decode(file_get_contents('https://graph.facebook.com/' . $fid));
		return $profile;
	}
	
	function get_name_from_id($fid) {
		
		$profile = $this->get_profile_from_id($fid);
		return $profile->name;
		
	}
	
	function show_name_from_id($fid) {
		//echo "function called";
		if($fid == 0) { return "The Man With No Name"; }
		else{
		$profile = $this->get_profile_from_id($fid);
		//print_r($profile);
		echo $profile->name; }
	}
	
	function get_name() {
		$profile = $this->get_profile();
		//print_r($profile);
		return $profile->name;
	}
	
	function show_name() {
		if($this->isLoggedIn()) { 
		$name = $this->get_name();
		echo $name; }
		else { echo "No User Logged In"; }
	}
	
	function start_facebook() {
		echo "<div id='fb-root'></div>";
		echo '<script src="http://connect.facebook.net/en_US/all.js"></script>';
	    echo '<script>
	      FB.init({appId: "117520721607819", status: true,
	               cookie: true, xfbml: true});
				FB.Event.subscribe("auth.login", function(response) {
				        window.location.reload();
				      });
		</script>';
	}


}
?>
